package com.hst.capacity.infra.persistence.jpa.order.pool;

import com.hst.capacity.domain.model.entity.order.intercity.IntercityOrderEntity;
import com.hst.capacity.domain.model.entity.order.pool.OrderPoolEntity;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

public interface OrderPoolRepository extends JpaRepository<OrderPoolEntity, String>, JpaSpecificationExecutor<OrderPoolEntity> {

    OrderPoolEntity findByIdOrder(String idOrder);

    @Query("select t from OrderPoolEntity t where t.externalOrderId = ?1")
    OrderPoolEntity findByExternalOrderId(String externalOrderId);

    @Query("select t from OrderPoolEntity t where t.status = ?1 and t.orderSource = ?2 order by t.createTime desc")
    List<OrderPoolEntity> findByStatus(int status, int channel);

    /**
     * 更新订单司机
     * @param idDriver
     * @param idOrder
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update OrderPoolEntity t set t.idDriver =:idDriver " +
            ", t.idFleet =:idFleet " +
            ", t.driverInfo =:driverInfo " +
            ", t.status =:status " +
            ", t.plateNumber =:plateNumber " +
            ", t.vehicleInfo =:vehicleInfo " +
            ", t.assignTime =:assignTime " +
            ", t.virtualOrderAccepted = 0 " +
            "where t.idOrder =:idOrder ")
    int updateDriver(@Param("idDriver") String idDriver, @Param("idFleet") String idFleet,
                     @Param("driverInfo") String driverInfo, @Param("status") int status,
                     @Param("plateNumber") String plateNumber, @Param("vehicleInfo") String vehicleInfo,
                     @Param("assignTime") LocalDateTime assignTime, @Param("idOrder") String idOrder);

    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update OrderPoolEntity t set t.idDriver =:idDriver " +
            ", t.virtualOrderAccepted = 1 " +
            "where t.idOrder =:idOrder ")
    int virtualAcceptOrder(String idOrder, String idDriver);

    /**
     * 更新订单系统派单时间
     * @param assignTime
     * @param idOrder
     * @return
     */
    @Modifying
    @Query("update OrderPoolEntity t set t.assignTime = ?2 where t.idOrder = ?1")
    int updateAssignTime(String idOrder, LocalDateTime assignTime);

    /**
     * 更新订单状态任务ID
     * @param idTask
     * @param idOrder
     * @return
     */
    @Modifying
    @Query("update OrderPoolEntity t set t.idTask = ?2,t.updateBy = ?3 where t.idOrder = ?1")
    int updateIdTask(String idOrder,String idTask,String updateBy);

    /**
     * 更新订单状态
     * @param orderStatus
     * @param idOrder
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update OrderPoolEntity t set t.status = ?2,t.updateBy = ?3 where t.idOrder = ?1")
    int updateOrderStatus(String idOrder,Integer orderStatus,String updateBy);

    /**
     * 根据司机代码查找订单
     * @param idDriver
     * @oaram idTask
     * @return
     */
    @Query("select t from OrderPoolEntity t where t.idDriver=:idDriver and t.idTask =:idTask and t.status between 0 and 5")
    List<OrderPoolEntity> findByIdDriverAndIdTask(@Param("idDriver")String idDriver, @Param("idTask")String idTask);

    /**
     * 更新订单用车类型（0：实时 1：预约）
     * @param orderType
     * @param idOrder
     * @return
     */
    @Modifying
    @Query("update OrderPoolEntity t set t.orderType = ?2 where t.idOrder = ?1")
    int updateOrderType(String idOrder,int orderType);

    /**
     * 根据司机ID统计未分账金额
     * @param idFranchisee
     * @return
     */
    @Query("select t from OrderPoolEntity t where t.idFranchisee = :idFranchisee and t.virtualOrderAccepted = 0 and (t.appointmentTime >= :startTime and t.appointmentTime <= :endTime)")
    List<OrderPoolEntity> dailyStat(@Param("idFranchisee")String idFranchisee, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 根据加盟商ID统计待调度订单
     * @param idFranchisee
     * @return
     */
    @Query("select t from OrderPoolEntity t where t.idFranchisee = :idFranchisee and (t.status < 2 or (t.status = 2 and t.virtualOrderAccepted = 1)) and t.appointmentTime <= :remindTime")
    List<OrderPoolEntity> orderReminder(@Param("idFranchisee")String idFranchisee, @Param("remindTime") LocalDateTime remindTime);

}
